﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionLayer;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace BusinessLayer
{
    public static class FacturaActions
    {
        public static bool EmitirFactura(String Obra, String oficina)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "exec dbo.sp_FacturarObra @Obra,@oficina";
                cmd.Parameters.AddWithValue("@Obra", Obra);
                cmd.Parameters.AddWithValue("@oficina", oficina);
                cmd.ExecuteScalar();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (Exception e) { MessageBox.Show(e.Message); }
            }
        }
        public static bool PagaFactura(String codObra, String codofic) {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "sp_PagarObra";
                SqlParameter ret = new SqlParameter("@res", System.Data.SqlDbType.Int);
                ret.Direction = System.Data.ParameterDirection.ReturnValue;
                cmd.Parameters.AddWithValue("@codO", codObra);
                cmd.Parameters.AddWithValue("@codOfic", codofic);
                cmd.Parameters.Add(ret);
                cmd.ExecuteNonQuery();
                if ((int)(ret.Value) == 0) {
                    return true;
                }
                else{
                    MessageBox.Show("A factura ainda nao esta emitida");
                    return false;
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }

        }
    }
}
